'use strick'

// 管理后台的所有 api

const Controller = require('egg').Controller

class MainController extends Controller {
  
  async index() {
    this.ctx.body = "hi,这是为 hotpot_blog 提供的后台api"
  }
  async Login() {

    console.log('使用 checklogin api -----');
    const user_phone = this.ctx.request.body.userName;
    const password = this.ctx.request.body.password;
    const sql = " SELECT user_phone FROM user_login WHERE user_phone = '" + user_phone +
                "' AND password = '" + password + "'";

    const res = await this.app.mysql.query(sql);
    console.log(user_phone, password, res, "-------------");
    if (res.length > 0) {
        // 登录成功,进行session缓存
        const openId = new Date().getTime()
        this.ctx.session.openId = { openId }
        this.ctx.body = { data: '登录成功', openId }
    } else {
        this.ctx.body = { data: '登录失败' }
    }
  }

  async checkLogin() {

  }
  
  // async getTypeInfo() {
  //   const resType = await this.app.mysql.select('type')
  //   console.log(resType, "??????????");
  //   this.ctx.body = { data: resType }
  // }

  // async addArticle() {
  //   const temArticle = this.ctx.request.body
  //   const result = await this.app.mysql.insert('article', temArticle)
  //   const insertSuccess = result.affectedRows === 1
  //   const insertId = result.insertId
  //   this.ctx.body = {
  //     isSuccess: insertSuccess,
  //     insertId
  //   }
  // }

  // async updateArticle() {
  //   const temArticle = this.ctx.request.body
  //   const result = await this.app.mysql.update('article', temArticle)
  //   const updateSuccess = result.affectedRows === 1
  //   this.ctx.body = {
  //     isSuccess: updateSuccess
  //   }
  // }

  // async getArticleList() {
  //   const sql = 'SELECT article.id as id,' +
  //   'article.title as title,' +
  //   'article.introduce as introduce,' +
  //   "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime," +
  //   'article.view_count as view_count ,' +
  //   'type.typeName as typeName ' +
  //   'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
  //   'ORDER BY article.id DESC '

  //   const resList = await this.app.mysql.query(sql)
  //   this.ctx.body = { list: resList }
  // }

  // async delArticle() {
  //   const id = this.ctx.params.id
  //   const res = await this.app.mysql.delete('article', { id })
  //   this.ctx.body = { data: res }
  // }

  // async getArticleById() {
  //   const id = this.ctx.params.id
  //   const sql = 'SELECT article.id as id,' +
  //   'article.title as title,' +
  //   'article.introduce as introduce,' +
  //   'article.article_content as article_content,' +
  //   "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime," +
  //   'article.view_count as view_count ,' +
  //   'type.typeName as typeName ,' +
  //   'type.id as typeId ' +
  //   'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
  //   'WHERE article.id=' + id
  //   const result = await this.app.mysql.query(sql)
  //   this.ctx.body = { data: result }
  // }
}

module.exports = MainController